﻿/// <reference path="tfcore.js"/>
/// <reference path="appobj.js"/>
// #pragma NoCompStart
/*
appcommon.js  1.0
specific functions for the GSF application content windows
*/
// #pragma NoCompEnd

/*global
HTMLElement, LocalOnLoad, Local_InitializePanel, Local_RefreshComplete, 
Local_RefreshRequest, edAddEvent, getViewPortHeight, getViewPortWidth, 
ig_Location, p_popError, xGetElementById, xGetElementsByClassName,
xHeight, xLeft, xMoveTo, xNextSib, xScrollLeft, xScrollTop, xStr, xTop, xWidth
*/


var setFocusOnLoad = null;
var warpMain = null; // needs to be changed to jswarpmain throughout code.

var alertTypes = { Error: 1, Warning: 2, Information: 3 };
var locTypes = { NW: 1, N: 2, NE: 3, E: 4, SE: 5, S: 6, SW: 7, W: 8, CEN: 9 };
var contentTypes = { iframe: 1, html: 2, alert: 3, MsgBox: 4 };
var msgboxType = { ok: 1, yes: 2, yesno: 3, yesnocancel: 4 };
var returnFunc = { pbContentPage: 1, pbPrevDlg: 2 };

debug = function(msg) { top.edNLog.debug(msg, "debug"); };

contentElem = function(elID) {
    return top.frames['ifMainContent']._gE(elID);
}

contentReload = function(){
        top._gE('ifMainContent').src = top._gE('ifMainContent').src;
}

contentPostBack = function(elTarget, elArgument) {
    top.frames['ifMainContent'].__doPostBack(elTarget, elArgument);
}

contentRedirect = function(newURL) {
    top._gE('ifMainContent').src = newURL;
}

//passthrough functions
function setFocus(elementId, frameName) {
    top.setFocus(elementId, frameName);
}

function elemStatus(strClientId, blnDisabled) {
   _gE(strClientId).disabled = blnDisabled;
}

function SetDialogTitle() {
    top.SetDialogTitle();
}
function ShowError(txtMessage, iType, callBackFunc) {
    top.ShowError(txtMessage, iType, callBackFunc);
}

    function elemReadOnly(strClientId, blnDisabled) {
        top.elemReadOnly(strClientId, blnDisabled);
    }
    
    function setCBByText(elemId, strDefault) {
        var sel = xGetElementById(elemId);
        for (var i = 0; i < sel.options.length; i++) {
            if (sel.options[i].text == strDefault) { sel.selectedIndex = i; }
        }
    }
    
    function setCBByValue(elemId, strDefault) {
        var sel = xGetElementById(elemId);
        for (var i = 0; i < sel.options.length; i++) {
            if (sel.options[i].value == strDefault) { sel.selectedIndex = i; }
        }
    }
    
// specific to Infragistics
function RefreshWarp(WarpPanelName) {
    ig_getWebControlById(WarpPanelName).refresh();
}
function warpPnl_RefreshRequest(oControl, oEvent, id) {
    //this.disabled = true;
    if (typeof top["hideError"] == 'function') { top.hideError(); }
    if (typeof Local_RefreshRequest == 'function') { Local_RefreshRequest(oControl, oEvent, id); }
}

function warpPnl_RefreshComplete(oPanel) {
    //this.disabled = false;
    if (typeof top.SessionRefresh == 'function') { top.SessionRefresh(); }  // Session Timeout
    if (typeof Local_RefreshComplete == 'function') { Local_RefreshComplete(oPanel); }
}

function warpPnl_InitializePanel(oPanel) {
    var pi = oPanel.getProgressIndicator();
    ig_shared.getCBManager()._timeLimit = 300000;
    pi.setImageUrl("/images/hourglass.gif"); //it can not be combined with setTemplate
    pi.setLocation(ig_Location.AboveCenter);
    if (typeof Local_InitializePanel == 'function') { Local_InitializePanel(oPanel); }

}
function doClick(buttonName,e)
{
    var key;
    if (window.event) { key = window.event.keyCode; }
    else {key = e.which;  }
    if (key == 13 || key == 9)
    {   var btn = xGetElementById(buttonName);
        if (btn != null) { btn.click();}
        e.stopPropagation && e.stopPropagation();
        e.preventDefault && e.preventDefault();
        e.cancelBubble = true;
        e.returnValue = false;
        e.keyCode = 0;
    }
}

function initializeCollapsible(showAll) {
    var i, icon, headings = xGetElementsByClassName('collapsible', document, 'h2');
    for (i = 0; i < headings.length; i++) {
        icon = document.createElement('div');
        icon.collapsibleSection = xNextSib(headings[i]);
        icon.onclick = iconOnClick;
        icon.onclick();
        if (showAll != true) {
            if (i > 0) icon.onclick();
        }

        icon.onmouseover = iconOnMouseover;
        icon.onmouseout = iconOnMouseout;
        headings[i].appendChild(icon);
    }
}

function iconOnClick() {
    var section = this.collapsibleSection;
    if (section.style.display != 'block') {
        section.style.display = 'block';
        this.className = 'CollapseIcon';
        this.title = 'Click to collapse';
    }
    else {
        section.style.display = 'none';
        this.className = 'ExpandIcon';
        this.title = 'Click to expand';
    }
}

function iconOnMouseover() {
    this.collapsibleSection.style.backgroundColor = '#999999';
}

function iconOnMouseout() {
    this.collapsibleSection.style.backgroundColor = 'transparent';
}

function iFrameKeyboardEvent(e)
	{
    var key;
        if(document.all) key=event.keyCode;
            else key=e.which;

        switch(key) {
        case 113:  //F2
    		try{top.OpenSearchDialog(top.SearchPage);}
    		catch(ex){alert("No Search Defined for screen");}
			event.keyCode = 0; //Stop browser bubble event!
            break;
        case 114: // F3
            top.swapPanel('dhtmlScanPanel');
			event.keyCode = 0; //Stop browser bubble event!
            break;
        case 118: // F7
            top.swapPanel('dhtmlScanPanel');
			event.keyCode = 0; //Stop browser bubble event!
            break;
        case 119: // F8
    		try{top.OpenSourceDialog(location);}
    		catch(ex1){alert("No Source found for screen: " + location.pathname);}
			event.keyCode = 0; //Stop browser bubble event!
            break;
        default:
            return true;
        }
        return false;
}



function fixEvent(event) {
	// add W3C standard event methods
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
}
fixEvent.preventDefault = function() {
	this.returnValue = false;
};
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};

//document.documentElement.onkeydown  = iFrameKeyboardEvent;

if(is.ff){
        HTMLElement.prototype.click = function() {
            var evt = document.createEvent('MouseEvents');
            evt.initMouseEvent('click', true, true, document.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
            this.dispatchEvent(evt);
        };
}

function stdOnUnload() { }
function stdOnload() {

    if (document.title != 'Session Timeout Warning') {
        var replaceTitle;
        if (document.forms[0]) { document.forms[0].disabled = false; }
        var metaTag = document.getElementsByName('MESTitle');
        if (metaTag.length > 0) { replaceTitle = metaTag[0].content; metaTag = null; }
        if (replaceTitle != "false" && top.g_jsTitle) {
            top.document.title = top.g_jsTitle.innerText = document.title;
        }
        else if (replaceTitle == "false" && top.g_jsTitle) {
            SetDialogTitle();
        }

        if (typeof top["CloseAllErrors"] == 'function') { top.CloseAllErrors(); }
        if (setFocusOnLoad != null) { setFocusOnLoad.focus(); }
        if (typeof top["SessionRefresh"] == 'function') { top.SessionRefresh(); }
        if (typeof window["LocalOnLoad"] == 'function') { document[LocalOnLoad()]; }
    }
}
edAddEvent(window, 'load', stdOnload);
//addDOMLoadEvent(stdOnload);
edAddEvent(window, 'unload', stdOnUnload);
shortcut.add("esc", top.closeModalDialog);
